<template>
    <div>
      <div class="outer">
        <div class="swiper-container">
          <div class="swiper-wrapper">
            <div class="swiper-slide" v-for="(item,index) in swiperImg" :key="index">
              <img :src="item" alt="">
            </div>
          </div>
          <div class="swiper-pagination"></div>
        </div>
        <div class="info">
          <h3>{{name}}</h3>
          <p>￥<span>{{price}}</span></p>
          <div class="intro" v-html="desc">

          </div>
        </div>
      </div>
      <div class="padding"/>
      <div class="btnbox">
        <button @click="onOrder">立即购买</button>
      </div>
    </div>
</template>

<script>
import Swiper from '../../static/js/swiper';
import Goods from '../api/goods/goods';
import LocalStorage from '../utils/local-storage';

export default {
  name: 'detail',
  data(){
    return {
      id: 0,
      price: '',
      swiperImg: [

      ],
      name: '',
      desc: ''
    }
  },

  mounted() {
    const id = this.$route.query.id;
    this.init(id);
    this.lunbo();
  },

  methods: {
    async init(id) {
      this.id = id;
      const detail = await Goods.getDetail(id);
      console.log(detail);
      this.swiperImg = detail.images.split(',');
      this.price = parseFloat(detail.price).toFixed(2);
      this.name = detail.name;
      this.desc = detail.description;
    },

    lunbo() {
      const mySwiper = new Swiper('.swiper-container',{
        pagination: {
          el: '.swiper-pagination',
          /*type: 'fraction'*/
        }
      })
    },

    onOrder() {
      const {id, swiperImg, price, name} = this;
      LocalStorage.setObject('order-goods', {
        id,
        image: swiperImg[0],
        price,
        name
      });

      this.$router.push({path: '/order', query:{id: id}})
    },
  },




}
</script>

<style scoped>
.outer{
  margin-bottom: 2rem;
}
.info{
  padding: .3rem;
}
.info h3{
  font-size: .4rem;
  color: #000;
}
.info p{
  color: #f00;
  font-size: .36rem;
  margin: .3rem 0 ;
}
.info p span{
  font-size: .44rem;
}
.info .intro{
  font-size: .36rem;
  line-height: .55rem;
  color: #333;
}
.btnbox{
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 1.3rem;
  text-align: center;
  line-height: 1.3rem;
  color: #fff;
  background: #f00;
}
.btnbox button{
  display: block;
  width: 100%;
  height: 1.3rem;
  background: #f00;
  color: #fff;
  border: none;
  font-size: .4rem;
  letter-spacing: 1px;
}
.swiper-slide img{
  display: block;
  width: 100%;
  height: 10rem;
}

.padding {
  width: 100vw;
  height: 1.3rem;
  opacity: 0;
}
</style>
